class FoodResource

Base class for all food resources contained in patches and consumed by bands. Contains a number of useful methods for harvesting and replenshing a resource. This class on its own is “abstract” and should not be instantiated.

Attributes

capacity[R]
carrying_capacity[R]
colour[R]
energy_harvested[R]
energy_value[R]
handle_time[R]
name[R]
repopulation_chance[R]
search_difficulty[R]
size[R]
tend_time[R]

Public Class Methods

new(cc, area) click to toggle source

Creates a new food resource instance with a certain carrying capacity (per/km^2) and an area in which the resource exists (km^2). This class on its own is “abstract” and should only be instantiated in subclasses.

# File lib/food_resource.rb, line 18
def initialize(cc, area)
  @capacity = @size = cc * area # Resources start at full capacity
  @carrying_capacity = cc
  # Chance per neighbouring healthy patch to repopulate from depletion (percent)
  @repopulation_chance = 10
  @tend_time = 0
  @name = :FoodResource
  @energy_harvested = 0.0
end

Public Instance Methods

available_energy() click to toggle source

Returns the total amount of energy available for consumption from this resource.

# File lib/food_resource.rb, line 53
def available_energy
  @size * @energy_value
end
depleted?() click to toggle source

If this resource has been entirely consumed then it is depleted.

# File lib/food_resource.rb, line 47
def depleted?
  @size <= 0.00001
end
grow() click to toggle source

Regrow the resource if it has been depleted. This is generally called by Scape

# File lib/food_resource.rb, line 59
def grow
  if @capacity - @size > 0.000001
    exponent = Math.exp(@growth_rate)
    se = @size * exponent
    @size = (se * @capacity) / (@capacity - @size + se)
  end
end
harvest(energy_wanted) click to toggle source

Attempt to harvest this resource for consumption. Harvesters “ask” for a certain number of resources (energy_wanted) and this method returns the number of calories that could be harvested from the resource. It also updates the quantity of the resource accordingly.

# File lib/food_resource.rb, line 32
def harvest(energy_wanted)
  quantity_wanted = energy_wanted / @energy_value
  @energy_harvested = 0
  if quantity_wanted > @size
    @energy_harvested = available_energy
    @size = 0
  else
    @energy_harvested = energy_wanted
    @size -= quantity_wanted
  end
  @energy_harvested
end
hash() click to toggle source

The hash code for resources is overloaded so that it is possible to cache pre-calculated NAR values. This works by including the size of the resource in the hash code calculation: if the size changes the cache for the NAR of the resource is invalidated.

# File lib/food_resource.rb, line 96
def hash
  [@size, self.object_id].hash
end
percent_full() click to toggle source

Returns the percentage of resource available; this is used for patch visualization.

# File lib/food_resource.rb, line 88
def percent_full
  @size * @energy_value
end
repopulate() click to toggle source

After depletion, resources can be repopulated; i.e. reset their size to the viable size.

# File lib/food_resource.rb, line 82
def repopulate
  @size = @viable_size
end
to_s() click to toggle source

Logging output

# File lib/food_resource.rb, line 69
def to_s
  "Name: #{@name} | Size: #{@size}/#{@capacity}"
end
viable?() click to toggle source

Returns true if this resource is dense enough to be what the researchers deemed “viable”. Viable patches have a chance of causing depleted resources in neighbouring patches to be repopulated.

# File lib/food_resource.rb, line 76
def viable?
  @size > @viable_size
end